#pragma once
#include "noncopyable.h"

#include <functional>
#include <memory>
#include <string>
#include <vector>

class EventLoop;
class EventLoopThread;

class EventLoopThreadPool : noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;
    EventLoopThreadPool(EventLoop *baseLoop, const std::string &nameArg);
    ~EventLoopThreadPool();

    void setThreadNum(int numThreads)
    {
        numThreads_ = numThreads;
    }

    void start(const ThreadInitCallback &cb = ThreadInitCallback());
    // 多线程中以轮询分配channel给subloop
    EventLoop *getNextLoop();

    std::vector<EventLoop *> getAllLoops();

    bool started() const
    {
        return started_;
    }
    const std::string name() const
    {
        return name_;
    }

private:
    std::string name_;
    bool started_;
    int numThreads_;
    int next_;
    EventLoop *baseLoop_;                                   // 主loop
    std::vector<std::unique_ptr<EventLoopThread>> threads_; // 线程队列
    std::vector<EventLoop *> loops_;                        // loop队列
};
